//
//  MainViewController.swift
//  YYWeiBo
//
//  Created by 崔洋洋 on 15/10/8.
//  Copyright © 2015年 崔洋洋. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        
        print(tabBar.subviews)
        

    }
    
    // 设置 "+" 按钮
   private func setupComposebutton() {
    
    let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
    let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
    composeButton.frame = CGRectOffset(rect, 2 * w
        , 0)
    
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
       
        setupComposebutton()
    }
    
    func clickButton() {
        print(__FUNCTION__)
    }
    
    // 添加子控制器
    private func addChildViewControllers() {
        addChildViewController( HomeTableViewController(), title: "首页", imageName:"tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName:"tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController( DiscoverTableViewController(), title: "发现", imageName:"tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName:"tabbar_profile")
        
    }
    
    // 设置子控制器属性
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        // 设置 tintcolor
        tabBar.tintColor = UIColor.orangeColor()

        // 从内向外传递设置
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    
    // 懒加载
    lazy private var composeButton: UIButton = {
        // 自定义类型按钮
        let button = UIButton()
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(button)
        
        // 监听方法
        button.addTarget(self, action:"clickButton" , forControlEvents:  UIControlEvents.TouchUpInside)
        
        return button
    }()
    
}
